package com.jic.point.controller;

import com.jic.point.model.entity.ConfPointCardBatch;
import com.jic.point.model.vo.request.ConfPointCardBatchRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import com.github.pagehelper.PageInfo;

import java.math.BigDecimal;
import java.util.List;

import com.jic.point.constant.consist.ResponseCode;

import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSONObject;
import com.jic.common.base.vo.RestResult;
import com.jic.point.model.vo.request.ConfPointCardRequest;
import com.jic.point.model.vo.response.ConfPointCardResponse;
import com.jic.point.model.entity.ConfPointCard;
import com.jic.point.service.ConfPointCardService;

/**
 * @Author: tzc
 * @date: 2020-10-11 17:28:32
 */
@RestController
@Slf4j
@RequestMapping("/confPointCard")
@Api(tags = " [积分卡列表]控制类-PC")
public class ConfPointCardController {
    @Autowired
    private ConfPointCardService service;


    /**
     * 新增记录
     *
     * @param request 新增[积分卡列表]请求参数
     * @return ConfPointCardResponse 新增[积分卡列表]响应参数
     * author tzc
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "新增[积分卡列表]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult<Long> save(@RequestBody ConfPointCardRequest request) throws Throwable {
        ConfPointCard confPointCard = new ConfPointCard();
        BeanUtils.copyProperties(request, confPointCard);
        long flag = service.save(confPointCard);
        return RestResult.success(flag);
    }

    /**
     * 新增或修改记录
     *
     * @param request 新增或修改[积分卡列表]请求参数
     * @return ConfPointCardResponse 新增或修改[积分卡列表]响应参数
     * author tzc
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "保存或更新[积分卡列表]")
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public RestResult<Long> saveOrUpdate(@Validated @RequestBody ConfPointCardRequest request) throws Throwable {
        long flag = service.saveOrUpdate(request);
        return RestResult.success(flag);
    }
    /**
     * 批量添加积分卡
     *
     * author fr
     * @date 2020-10-29
     */
    @ApiOperation(value = "批量生成积分卡")
    @PostMapping("volumeProduction")
    @ResponseBody
    public RestResult<Integer> volumeProductionCardPoint(@RequestBody ConfPointCardBatchRequest request){
        BigDecimal bigDecimal = new BigDecimal(15000);
        request.getList().stream().map(item->{
            if (item.getTotalNum().compareTo(bigDecimal)==1||item.getLeftNum().compareTo(bigDecimal)==1) {
                return RestResult.error("8888","批量开卡的积分卡最多不能错过15000张");
            }
            return null;
        }).collect(Collectors.toList());
        Integer flag = service.volumeProductionCardPoint(request);
        if (flag>0){
            return RestResult.success(flag);
        }
        return RestResult.error("9999","批量生成失败");
    }



    /**
     * 删除记录
     *
     * @param request 删除[积分卡列表]请求参数
     * @return ConfPointCardResponse 删除[积分卡列表]响应参数
     * author tzc
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "删除[积分卡列表]记录")
    @PostMapping("/delete")
    @ResponseBody
    public RestResult<Long> delete(@RequestBody ConfPointCardRequest request) throws Throwable {
        ConfPointCard confPointCard = new ConfPointCard();
        BeanUtils.copyProperties(request, confPointCard);
        long flag = service.deleteByPrimaryKeyLogically(confPointCard);
        return RestResult.success(flag);
    }

    /**
     * 修改记录
     *
     * @param request 修改[积分卡列表]请求参数
     * @return ConfPointCardResponse 修改[积分卡列表]响应参数
     * author tzc
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "修改[积分卡列表]记录")
    @PostMapping("/update")
    @ResponseBody
    public RestResult<Long> update(@RequestBody ConfPointCardRequest request) throws Throwable {
        ConfPointCard confPointCard = new ConfPointCard();
        BeanUtils.copyProperties(request, confPointCard);
        long flag = service.updateByPrimaryKeySelective(confPointCard);
        return RestResult.success(flag);
    }

    /**
     * 单表查询详情信息
     *
     * @param request 查询[积分卡列表]详情请求参数
     * @return ConfPointCardResponse 查询[积分卡列表]详情响应参数
     * author tzc
     * @date 2020-3-11 20:54:04
     */
    @PostMapping("/getByPrimaryKey")
    @ApiOperation(value = "查询[积分卡列表]详情")
    @ResponseBody
    public RestResult<ConfPointCardResponse> getByPrimaryKey(@Validated @RequestBody ConfPointCardRequest request) {
        log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
        if (request != null && request.getId() != null) {

            ConfPointCard confPointCard = new ConfPointCard();
            BeanUtils.copyProperties(request, confPointCard);
            ConfPointCard result = service.getByPrimaryKey(confPointCard);
            log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
            if (result == null) {
                return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
            }
            ConfPointCardResponse bo = new ConfPointCardResponse();
            BeanUtils.copyProperties(result, bo);
            return RestResult.success(bo);
        }
        log.info("查询详情请求结束");
        return RestResult.success(new ConfPointCardResponse());
    }

    /**
     * 分页查询
     *
     * @param request 分页查询请求参数
     * @return 分页列表
     * @throws Throwable 业务异常和系统异常
     */
    @PostMapping("/listPage")
    @ApiOperation(value = " 分页查询列表 ")
    @ResponseBody
    public RestResult<PageInfo<ConfPointCard>> listPage(@RequestBody ConfPointCardRequest request) throws Throwable {
        ConfPointCard param = new ConfPointCard();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        return RestResult.success(service.listPageBySelective(param, request.getPageNum(), request.getPageSize()));
    }


    /**
     * CONF_POINT_CARD 查询列表（不分页）
     *
     * @param request 前端请求参数
     */
    @PostMapping("/listAll")
    @ApiOperation(value = "不分页查询 [积分卡列表] ")
    @ResponseBody
    public RestResult<List<ConfPointCardResponse>> listConfPointCardAllRecord(@RequestBody ConfPointCardRequest request) throws Throwable {
        ConfPointCard param = new ConfPointCard();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        List<ConfPointCard> list = service.listAllRecord(param);
        List<ConfPointCardResponse> responseList = list.stream().map(temp -> {
            ConfPointCardResponse bo = new ConfPointCardResponse();
            BeanUtils.copyProperties(temp, bo);
            return bo;
        }).filter(item -> item != null).collect(Collectors.toList());
        return RestResult.success(responseList);
    }

    @PostMapping("generateIntegralCard")
    @ApiOperation(value = "生成积分卡")
    @ResponseBody
    public RestResult<ConfPointCardResponse>  generateIntegralCard(@RequestBody ConfPointCardBatchRequest request){
        ConfPointCardResponse response = service.generateIntegralCard(request);
        return RestResult.success(response);
    }


    @PostMapping("count")
    @ApiOperation(value = "条数")
    public RestResult<List<ConfPointCardResponse>> count(){
        List<ConfPointCardResponse> list=service.count();
        return RestResult.success(list);
    }

}

	
